package com.glaivelee.abstracts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.glaivelee.interfaces.IChooseType;

public abstract class chooseType implements IChooseType {
	protected String srcValue = "";
	protected int ImagesCount = 1;
	protected static List<String> regexList = new ArrayList<String>();
	protected static List<String> regexListUnNamed = new ArrayList<String>();
	protected static List<String> clearList = new ArrayList<String>();
	protected static Map<String, String> pair = new HashMap<String, String>();

	private chooseType() {
	}

	public static void dataInit() {
		regexList.add(getHtmlLabelExpression2("p"));
		regexList.add(getHtmlLabelExpression2("div"));
		regexList.add(getHtmlLabelExpression2("table"));

		regexList.add(getHtmlLabelExpression2("span"));
		regexList.add(getHtmlLabelExpression2("img"));
		
		regexListUnNamed.add(getHtmlLabelExpressionUnNamed("p"));
		regexListUnNamed.add(getHtmlLabelExpressionUnNamed("div"));
		regexListUnNamed.add(getHtmlLabelExpressionUnNamed("table"));

		regexListUnNamed.add(getHtmlLabelExpressionUnNamed("span"));
		regexListUnNamed.add(getHtmlLabelExpressionUnNamed("img"));
		
		clearList.add("<br>");
		clearList.add("<br/>");
		clearList.add("<br />");
		clearList.add("\r");
		clearList.add("\n");
		clearList.add("\r\n");
		clearList.add("【转】");

		pair.put("glaivelee/glaivelee", "glaivelee");
	}

	public chooseType(String srcValue) {
		this.srcValue = srcValue;
//		dataInit();
//		filterUselessWords();
		replaceStr();
	}

	public static void main(String[] args) {
		dataInit();
		// Iterator<String> itr = regexList.iterator();
		// while (itr.hasNext()) {
		// String nextRegex = getHtmlLabelExpression2(itr.next());
		// System.out.println(nextRegex);
		// }

		System.out
				.println("-->"
						+ ClearSpace("&nbsp;   helloworld&nbsp;   blackhorse  &nbsp;")
						+ "<--");
	}

	public String exeRex() {
		return null;
	}

	// Regex start
	public String exeRex(ArrayList<String> patternList) {
		patternList.add("<a\\s{0,}([^>]+)>(?<linkContent>(.*?))</a>");
		patternList
				.add("<a\\s{0,}(href=\".*?\")[^>]+>(?<linkContent>(.*?))</a>");
		int xx = 0;
		for (int j = 0; j < patternList.size(); j++) {
			String filterStr = "";
			Pattern pattern = Pattern.compile(patternList.get(j));
			Matcher matcher = pattern.matcher(srcValue);
			while (matcher.find()) {
				String spaceStrClear = clearHtmlLabels2(matcher
						.group("linkContent"));
				filterStr += "<a " + matcher.group(1) + "target=\"_blank\">"
						+ spaceStrClear + "</a><br><br>";
			}
			srcValue = filterStr;
		}
		return srcValue;
	}

	public void filterUselessWords() {
		// 过滤无用字符
		for (int i = 0; i < clearList.size(); i++) {
			Pattern p = Pattern.compile(clearList.get(i), 1);
			Matcher m = p.matcher(srcValue); // 获得匹配器对象
			srcValue = m.replaceAll("");
		}
	}

	private void replaceStr() {
		for (Object key : pair.keySet()) {
			replaceByKey(key);
		}
	}

	private void replaceByKey(Object RegexKey) {
		while (true) {
			Pattern p = Pattern.compile(RegexKey.toString(), 1);
			Matcher m = p.matcher(srcValue); // 获得匹配器对象
			if (!m.find()) {
				break;
			}
			srcValue = m.replaceAll(pair.get(RegexKey)).trim();
		}
	}

	public static String ClearSpace(String ssc) {
		String space = "&nbsp;";
		String space2 = " ";
		while (true) {
			if (ssc.startsWith(space)) {
				ssc = ssc.substring(space.length());
			}
			if (ssc.startsWith(space2)) {
				ssc = ssc.substring(space2.length());
			}
			if (ssc.endsWith(space) && (ssc.length() >= space.length())) {
				ssc = ssc.substring(0, ssc.length() - space.length());
			}
			if (ssc.endsWith(space2) && (ssc.length() >= space.length())) {
				ssc = ssc.substring(0, ssc.length() - space2.length());
			}
			if (!ssc.startsWith(space) && !ssc.endsWith(space)
					&& !ssc.startsWith(space2) && !ssc.endsWith(space2)) {
				break;
			}
		}
		return ssc;
	}

	public static String ClearSpaceStrFromLeftAndRight(String ssc, String subStr) {
		String space = ("".equals(subStr)) ? "&nbsp;" : subStr;
		while (true) {
			if (ssc.startsWith(space)) {
				ssc = ssc.substring(space.length());
			}
			if (ssc.endsWith(space) && (ssc.length() >= space.length())) {
				ssc = ssc.substring(0, ssc.length() - space.length());
			}
			if (ssc.startsWith(space) || ssc.endsWith(space)) {
				continue;
			} else {
				break;
			}
		}
		return ssc;
	}

	public static String getHtmlLabelExpression(String labelStr) {
		if ("img".equals(labelStr)) {
			return "<" + labelStr + ".*?" + "/>";
		}
		return "<" + labelStr + ".*?" + "</" + labelStr + ">";
	}

	// Regex start
	public String clearStyleByPattern(String... patternList) {
		int xx = 0;
		for (int j = 0; j < patternList.length; j++) {
			String filterStr = "";
			Pattern pattern = Pattern.compile(patternList[j]);
			Matcher matcher = pattern.matcher(srcValue);

			while (matcher.find()) {
				srcValue = matcher.replaceAll("").trim();
				// HTMLFilter.setSource(srcValue);
				// HTMLFilter.run();
				// filterStr = HTMLFilter.getResult();
				// System.out.println(filterStr);
				// xx++;
			}
			// srcValue = filterStr;
		}
		return srcValue;
	}
	
	/**
	 * @param groupStr
	 * @return
	 */
	public static String clearHtmlLabels2(String groupStr) {
		Iterator<String> itr = regexList.iterator();
		while (itr.hasNext()) {
			String nextRegex = itr.next();
			Pattern p = Pattern.compile(nextRegex, 1);
			Matcher m = p.matcher(groupStr);
			if(m.find()){
				if("".equals(ClearSpace(m.group("content")))){
					groupStr = m.replaceAll("").trim();
				}
			}
		}
		return ClearSpace(groupStr);
	}

	public static String getHtmlLabelExpression2(String labelStr) {
		if ("img".equals(labelStr)) {
			return "<" + labelStr + "(?<style>(.*?))" + "/>";
		}
		return "<" + labelStr + "(?<style>(.*?))" + ">(?<content>(.*?))</"
				+ labelStr + ">";
	}
	
	public static String getHtmlLabelExpressionUnNamed(String labelStr) {
		if ("img".equals(labelStr)) {
			return "<" + labelStr + "(.*?)" + "/>";
		}
		return "<" + labelStr + "(.*?)" + ">(.*?)</"
				+ labelStr + ">";
	}
}
